import {createRouter, createWebHashHistory} from 'vue-router'

// 静态路由
const routes = [
    {
        path: '',
        name: 'layout',
        component: () => import('@/views/layout/index.vue'),
        redirect: '/dashboard',
        children: [
            {
                path: '/dashboard',
                name: 'dashboard',
                component: () => import('@/views/dashboard/index.vue'),
                meta: {title: '首页'}
            },
            {
                path: '/personal/profile',
                component: () => import('@/views/personal/profile/index.vue'),
                meta: {title: '个人信息'}
            },
            {
                path: '/personal/changePassword',
                component: () => import('@/views/personal/change_password/index.vue'),
                meta: {title: '修改密码'}
            },
            {
                path: '/system/user',
                component: () => import('@/views/system/user/index.vue'),
                meta: {title: '用户管理'}
            },
            {
                path: '/system/role',
                component: () => import('@/views/system/role/index.vue'),
                meta: {title: '角色管理'}
            },
            {
                path: '/system/menu',
                component: () => import('@/views/system/menu/index.vue'),
                meta: {title: '菜单管理'}
            },
            {
                path: '/system/dict',
                component: () => import('@/views/system/dict/index.vue'),
                meta: {title: '字典管理'}
            },
            {
                path: '/system/dict/option',
                component: () => import('@/views/system/dict/option/index.vue'),
                meta: {title: '字典项列表'}
            },
            {
                path: '/system/log/operate',
                component: () => import('@/views/system/log/OperateLog.vue'),
                meta: {title: '操作日志'}
            },
            {
                path: '/system/log/login',
                component: () => import('@/views/system/log/LoginLog.vue'),
                meta: {title: '登录日志'}
            }
        ]
    },
    {
        path: '/login',
        component: () => import('@/views/login/index.vue')
    },
    {
        path: '/403',
        component: () => import('@/views/_error/403.vue')
    },
    {
        path: '/404',
        component: () => import('@/views/_error/404.vue')
    },
    {
        path: '/500',
        component: () => import('@/views/_error/500.vue')
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
    scrollBehavior: () => {
        return {top: 0}
    }
})

// 免登录白名单
const whiteList = ['/login', '/403', '/404', '/500']

// 路由导航前置守卫
router.beforeEach(async (to, from, next) => {
    // 设置页面标题
    document.title = to.meta.title || 'car-rental'
    // 判断是否登录
    if (!localStorage.getItem('token')) {
        // 在免登录白名单中的直接放行，其它的重定向到登录页面
        whiteList.includes(to.path) ? next() : next(`/login?redirect=${to.path}`)
        return
    }
    // 如果没有找到匹配的路由，转到404页面，否则直接进入
    to.matched.length === 0 ? next('/404') : next()
})

export default router
